package com.kehutong.question.service;

import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;
import com.kehutong.question.entity.QuestionCommon;
import com.kehutong.question.entity.QuestionCommonClassify;

@WebService("/question/service")
public class QuestionCommonService {

    @Inject
    private JdbcSession jdbcSession;

    public Object list(JSONObject jsonObject) {
        FindPage<QuestionCommon> findPage = jdbcSession.findPage(QuestionCommon.class);
        if(jsonObject.getBooleanValue("showStatus")) {
            findPage.eq("showStatus", true);
        }

        return Result.success(findPage
            .eq(jsonObject, "classifyId")
            .like(jsonObject, "title")
            .eq("deleted", false)
            .order("showStatus", "desc")
            .order("sort", "asc")
            .page(jsonObject)
            .exe());
    }

    public Object save(QuestionCommon question) {
        setClassifyName(question);

        jdbcSession.insert(question);

        return Result.success();
    }

    private void setClassifyName(QuestionCommon question) {
        QuestionCommonClassify classify = Pool.get(QuestionCommonClassify.class, question.getClassifyId());
        if (Objects.nonNull(classify)) {
            question.setClassifyName(classify.getName());

            QuestionCommon oldCommon = Pool.get(QuestionCommon.class, question.getId());
            if (Objects.isNull(oldCommon)){
                int count = classify.getCount() + 1;
                classify.setCount(count);
                jdbcSession.updateById(classify);
            } else if (!question.getClassifyId().equals(oldCommon.getClassifyId())){
                int count = classify.getCount() + 1;
                classify.setCount(count);
                jdbcSession.updateById(classify);

                QuestionCommonClassify oldClassify = Pool.get(QuestionCommonClassify.class, oldCommon.getClassifyId());
                if (Objects.nonNull(oldClassify)) {
                    int oldCount = oldClassify.getCount() - 1;
                    oldClassify.setCount(oldCount);
                    jdbcSession.updateById(oldClassify);
                }
            } else {
                // continue
            }

        }
    }

    public Object update(QuestionCommon question) {
        setClassifyName(question);
        jdbcSession.updateById(question);

        return Result.success();
    }

    public Object delete(QuestionCommon question) {
        question.setDeleted(true);
        jdbcSession.updateById(question);

        QuestionCommonClassify classify = Pool.get(QuestionCommonClassify.class, question.getClassifyId());
        int count = classify.getCount() - 1;
        classify.setCount(count);
        jdbcSession.updateById(classify);

        return Result.success();
    }

    public Object detail(QuestionCommon question) {
        if (Objects.isNull(question)) {
            return com.kehutong.common.errcode.Result.error(0, "该常见问题不存在");
        }

        // 阅读数+1
        int readNum = question.getReadNum() + 1;
        question.setReadNum(readNum);
        jdbcSession.updateById(question);

        return Result.success(question);

    }

}
